package org.invigorare.auctions.data;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.*;

import static org.invigorare.auctions.data.SqlTypes.*;
import org.invigorare.auctions.data.entities.Licensee;

public class LicenseDataOpener extends SynchronizableInboundDataOpener {

	private static final int DATABASE_VERSION = 8;

	public static final String LICENSE_TABLE_NAME 	= "license_table";
	public static final String LICENSE_TABLE_NULL_COL	= "nullIfNoData";	
	
	public static final String _ID					= "_id"; // Column Name must be exactly so, for Query Adapters to work
	public static final String LICENSEE_NAME		= "name"; 
	public static final String LICENSEE_ADDR_STREET	= "addr_street";
	public static final String LICENSEE_ADDR_STATE	= "addr_state";
	public static final String LICENSEE_ADDR_PIN	= "addr_pincode";
	public static final String LICENSE_SELLER_LIC	= "seller_lic";
	public static final String LICENSE_BUYER_LIC	= "buyer_lic";
	public static final String LICENSEE_ORG			= "organization";

	private static final ColBinding[] columnBindings = new ColBinding[] {
		new ColBinding(_ID, 					INT_KEY),
		
		new ColBinding(LICENSEE_NAME, 			TEXT),
		new ColBinding(LICENSEE_ADDR_STREET,	TEXT),
		new ColBinding(LICENSEE_ADDR_STATE,		TEXT),
		new ColBinding(LICENSEE_ADDR_PIN,		TEXT),
		new ColBinding(LICENSEE_ORG,			TEXT),
		
		new ColBinding(LICENSE_SELLER_LIC,		TEXT),
		new ColBinding(LICENSE_BUYER_LIC,		TEXT)
	};
	
	public LicenseDataOpener(Context context) {
		super(context, LICENSE_TABLE_NAME, null, DATABASE_VERSION, LICENSE_TABLE_NULL_COL);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(createTableString(LICENSE_TABLE_NAME, columnBindings));
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
		// TODO right now, just discards the old db and makes a new one
		db.execSQL("DROP TABLE " + LICENSE_TABLE_NAME);
		db.execSQL(createTableString(LICENSE_TABLE_NAME, columnBindings));
	}
	
	public static SQLiteDatabase getWritableDb(Context context) {
		LicenseDataOpener dbHelper = new LicenseDataOpener(context);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		return db;
	}
	
	public static SQLiteDatabase getReadableDb(Context context) {
		LicenseDataOpener dbHelper = new LicenseDataOpener(context);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		return db;
	}

	/** Opens the License Table from the database and returns 
	 * a List of Licensees from the rows it contains.
	 * 
	 * @param context the Application Context containing the desired database
	 * @return
	 */
	public static List<Licensee> getLicensees(Context context) {
		
		SQLiteDatabase db = getReadableDb(context);
		Log.i("LicenseDataOpener", "Opening DB to get list of licensees.");
		
		// The Columns we want to fetch
		final String[] columns = {
			LICENSEE_NAME,
			LICENSEE_ADDR_STREET,
			LICENSEE_ADDR_STATE,
			LICENSEE_ADDR_PIN,
			
			LICENSE_SELLER_LIC,
			LICENSE_BUYER_LIC,
			
			LICENSEE_ORG
		};
		
		final int NAME_COL = 0;
		final int ADDR_STREET_COL = 1;
		final int ADDR_STATE_COL = 2;
		final int ADDR_PIN_COL = 3;
		final int SELLER_LIC_COL = 4;
		final int BUYER_LIC_COL = 5;
		final int ORG_COL = 6;
		
		// Pull all the data and put it into the desired data structure
		Cursor query = db.query(LICENSE_TABLE_NAME, columns, null, null, null, null, null);

		List<Licensee> list = new ArrayList<Licensee>(query.getCount()); 
		while (query.moveToNext()) {
			Licensee licensee = new Licensee(
					query.getString(NAME_COL),
					query.getString(ADDR_STREET_COL),
					query.getString(ADDR_STATE_COL),
					query.getString(ADDR_PIN_COL),
					query.getString(SELLER_LIC_COL),
					query.getString(BUYER_LIC_COL),
					query.getString(ORG_COL)
					);
			list.add(licensee);
		}
		
		// Cleanup and return
		Log.i("LicenseDataOpener", "Successfully got list of " + query.getCount() + " produce names.");
		query.close();
		db.close();
		return list;
	}
}
